/*
 * Copyright (c) 2002-2007, Mairie de Paris
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice
 *     and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice
 *     and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *
 *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * License 1.0
 */
package fr.paris.lutece.plugins.customisation.business.portlet;

import fr.paris.lutece.portal.business.portlet.Portlet;
import fr.paris.lutece.portal.service.spring.SpringContextService;

import java.sql.Timestamp;

import java.util.List;


/**
 * This class provides instances management methods (create, find, ...) for PersonalPortlet objects
 */
public final class PersonalPortletHome
{
    // Static variable pointed at the DAO instance
    private static IPersonalPortletDAO _dao = (IPersonalPortletDAO) SpringContextService.getPluginBean( "customisation",
            "personalPortletDAO" );

    /**
     * Private constructor - this class need not be instantiated
     */
    private PersonalPortletHome(  )
    {
    }

    /**
     * add a new record in the table
     *
     * @param portlet the personal portlet to add in the database
     */
    public static void create( PersonalPortlet portlet )
    {
        _dao.insert( portlet );
    }

    /**
     * Deletes the portlet in the database
     *
     * @param portlet the portlet to remove
     */
    public static void remove( PersonalPortlet portlet )
    {
        _dao.delete( portlet );
    }
    
    /**
     * Delete the records of a specific tab from the table
     *
     * @param portlet the portlet to delete of the specific tab
     */
    public static void removeAllPortletsByTab( PersonalPortlet portlet )
    {
        _dao.deleteAllPortletByTab( portlet );
    }

    /**
     * Updates a portlet with the values of the specified portlet instance
     *
     * @param portlet portlet to update
     */
    public static void update( PersonalPortlet portlet )
    {
        _dao.store( portlet );
    }

    /**
     * Returns the PersonalPortlet whose primary key is specified in parameter, and the tab identifier where he is
     *
     * @param nIdPortlet the portlet identifier
     * @param nIdTab the tab identifier
     * @return The portlet object
     */
    public static PersonalPortlet findByPrimaryKeyTabKey( int nIdPortlet, int nIdTab )
    {
        return _dao.load( nIdPortlet, nIdTab );
    }

    /**
     * Returns a list of portlets according the user identifier and the tab identifier
     *
     * @param strUserName the user identifier
     * @param nIdTab the tab identifier
     * @return the user's portlets in form of Collection
     */
    public static List<PersonalPortlet> findByPrimaryTabKey( String strUserName, int nIdTab )
    {
        return _dao.selectAllPortletByTab( strUserName, nIdTab );
    }

    /**
     * Returns the list of portlets used by users' site
     *
     * @return the list of portlets used by users' site
     */
    public static List<PersonalPortlet> getAllPortletsUsed(  )
    {
        return _dao.selectAllPortlets(  );
    }

    /**
     * Returns the count of portlet's presence in the front-office
     *
     * @param nIdPortlet the portlet identifier
     * @return the count of portlet's presence in the front-office
     */
    public static int getCountPortlet( int nIdPortlet )
    {
        return _dao.selectCountPortlet( nIdPortlet );
    }

    /**
     * Returns the count of a specified portlet by the different user
     *
     * @param nIdPortlet the portlet identifier
     * @return the count of a specified portlet by the different user
     */
    public static int getCountPortletByUser( int nIdPortlet )
    {
        return _dao.selectCountPortletByUser( nIdPortlet );
    }

    /**
     * Returns the date of the last add for a specified portlet
     *
     * @param nIdPortlet the portlet identifier
     * @return the date of the last add for a specified portlet
     */
    public static Timestamp getLastAddition( int nIdPortlet )
    {
        return _dao.selectLastAddition( nIdPortlet );
    }

    /**
     * Deletes the portlet specified in all the tabs of users' site
     *
     * @param nPortletId the portlet identifier
     */
    public static void deletePortletOnAllTab( int nPortletId )
    {
        _dao.deletePortletFromAllTab( nPortletId );
    }

    /**
     * Returns if the portlet specified exist or not
     *
     * @param nIdPortlet the portlet identifier
     * @return if the portlet specified exist or not
     */
    public static boolean checkExistenceOfPortlet( int nIdPortlet )
    {
        return _dao.checkPortlet( nIdPortlet );
    }

    /**
     * Update the records in the table
     *
     * @param portlet the portlet to update in the database
     */
    public static void update( Portlet portlet )
    {
        _dao.store( portlet );
    }
    
    /**
     * Returns the list of published portlets but not displayed on any page
     *
     * @param the identifier of the tab
     * @return the undisplayed portlets in form of Collection
     */
    public static List<PersonalPortlet> getUndisplayedPortletList( int nIdTab)
    {	
        return _dao.selectAllUndisplayedPortlet( nIdTab );
    }
}
